home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / unittest / util.pyc (.txt) < prev   
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  188 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Various utility functions.'''
  5. from collections import namedtuple, OrderedDict
  6. __unittest = True
  7. _MAX_LENGTH = 80
  8.  
  9. def safe_repr(obj, short = False):
  10.     
  11.     try:
  12.         result = repr(obj)
  13.     except Exception:
  14.         result = object.__repr__(obj)
  15.  
  16.     if not short or len(result) < _MAX_LENGTH:
  17.         return result
  18.     return None[:_MAX_LENGTH] + ' [truncated]...'
  19.  
  20.  
  21. def strclass(cls):
  22.     return '%s.%s' % (cls.__module__, cls.__name__)
  23.  
  24.  
  25. def sorted_list_difference(expected, actual):
  26.     '''Finds elements in only one or the other of two, sorted input lists.
  27.  
  28.     Returns a two-element tuple of lists.    The first list contains those
  29.     elements in the "expected" list but not in the "actual" list, and the
  30.     second contains those elements in the "actual" list but not in the
  31.     "expected" list.    Duplicate elements in either input list are ignored.
  32.     '''
  33.     i = j = 0
  34.     missing = []
  35.     unexpected = []
  36.     while True:
  37.         
  38.         try:
  39.             e = expected[i]
  40.             a = actual[j]
  41.             if e < a:
  42.                 missing.append(e)
  43.                 i += 1
  44.                 while expected[i] == e:
  45.                     i += 1
  46.             elif e > a:
  47.                 unexpected.append(a)
  48.                 j += 1
  49.                 while actual[j] == a:
  50.                     j += 1
  51.             else:
  52.                 i += 1
  53.                 
  54.                 try:
  55.                     while expected[i] == e:
  56.                         i += 1
  57.                 finally:
  58.                     j += 1
  59.                     while actual[j] == a:
  60.                         j += 1
  61.  
  62.         continue
  63.         except IndexError:
  64.             missing.extend(expected[i:])
  65.             unexpected.extend(actual[j:])
  66.             break
  67.             continue
  68.         
  69.  
  70.     return (missing, unexpected)
  71.  
  72.  
  73. def unorderable_list_difference(expected, actual, ignore_duplicate = False):
  74.     '''Same behavior as sorted_list_difference but
  75.     for lists of unorderable items (like dicts).
  76.  
  77.     As it does a linear search per item (remove) it
  78.     has O(n*n) performance.
  79.     '''
  80.     missing = []
  81.     unexpected = []
  82.     while expected:
  83.         item = expected.pop()
  84.         
  85.         try:
  86.             actual.remove(item)
  87.         except ValueError:
  88.             missing.append(item)
  89.  
  90.         if ignore_duplicate:
  91.             for lst in (expected, actual):
  92.                 
  93.                 try:
  94.                     while True:
  95.                         lst.remove(item)
  96.                 continue
  97.                 except ValueError:
  98.                     continue
  99.                 
  100.  
  101.             
  102.         if ignore_duplicate:
  103.             while actual:
  104.                 item = actual.pop()
  105.                 unexpected.append(item)
  106.                 
  107.                 try:
  108.                     while True:
  109.                         actual.remove(item)
  110.                 continue
  111.                 except ValueError:
  112.                     continue
  113.                 
  114.  
  115.             return (missing, unexpected)
  116.         return (None, actual)
  117.  
  118. _Mismatch = namedtuple('Mismatch', 'actual expected value')
  119.  
  120. def _count_diff_all_purpose(actual, expected):
  121.     '''Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'''
  122.     s = list(actual)
  123.     t = list(expected)
  124.     m = len(s)
  125.     n = len(t)
  126.     NULL = object()
  127.     result = []
  128.     for i, elem in enumerate(s):
  129.         if elem is NULL:
  130.             continue
  131.         cnt_s = cnt_t = 0
  132.         for j in range(i, m):
  133.             if s[j] == elem:
  134.                 cnt_s += 1
  135.                 s[j] = NULL
  136.                 continue
  137.         for j, other_elem in enumerate(t):
  138.             if other_elem == elem:
  139.                 cnt_t += 1
  140.                 t[j] = NULL
  141.                 continue
  142.         if cnt_s != cnt_t:
  143.             diff = _Mismatch(cnt_s, cnt_t, elem)
  144.             result.append(diff)
  145.             continue
  146.     for i, elem in enumerate(t):
  147.         if elem is NULL:
  148.             continue
  149.         cnt_t = 0
  150.         for j in range(i, n):
  151.             if t[j] == elem:
  152.                 cnt_t += 1
  153.                 t[j] = NULL
  154.                 continue
  155.         diff = _Mismatch(0, cnt_t, elem)
  156.         result.append(diff)
  157.     
  158.     return result
  159.  
  160.  
  161. def _ordered_count(iterable):
  162.     '''Return dict of element counts, in the order they were first seen'''
  163.     c = OrderedDict()
  164.     for elem in iterable:
  165.         c[elem] = c.get(elem, 0) + 1
  166.     
  167.     return c
  168.  
  169.  
  170. def _count_diff_hashable(actual, expected):
  171.     '''Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'''
  172.     s = _ordered_count(actual)
  173.     t = _ordered_count(expected)
  174.     result = []
  175.     for elem, cnt_s in s.items():
  176.         cnt_t = t.get(elem, 0)
  177.         if cnt_s != cnt_t:
  178.             diff = _Mismatch(cnt_s, cnt_t, elem)
  179.             result.append(diff)
  180.             continue
  181.     for elem, cnt_t in t.items():
  182.         if elem not in s:
  183.             diff = _Mismatch(0, cnt_t, elem)
  184.             result.append(diff)
  185.             continue
  186.     return result
  187.  
  188.